#include "fishwidget.h"

#include <QPainter>

FishWidget::FishWidget(QWidget *parent) : QWidget(parent)
{
    m_pixamp = QPixmap(":/images/fish-svg1.svg");
    connect(&timer, &QTimer::timeout, [=](){
        if (index == 5)
            isOk = false;
        if (index == 1)
            isOk = true;
        isOk ? index++ : index--;
        m_pixamp = QPixmap(QString(":/images/fish-svg%1.svg").arg(index));
        this->update();
    });

    timer.start(100);
}

void FishWidget::setPixmap(const QPixmap &pixmap)
{
    if (m_pixamp != pixmap) {
        m_pixamp = pixmap;
        update();
    }
}

void FishWidget::setAngle(qreal angle)
{
    if (m_angle != angle) {
        m_angle = angle;
        update();
    }
}

void FishWidget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
    painter.translate(rect().center().x(), rect().center().y());
    painter.rotate(m_angle);
    painter.translate(-rect().center().x(),-rect().center().y());
    painter.drawPixmap(rect().topLeft(), m_pixamp);
}

QSize FishWidget::sizeHint() const
{
    return m_pixamp.size();
}
